package com.smedi.ismedi.authority.starter.util;

import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static com.smedi.ismedi.authority.starter.common.Constant.REDIS_DEFAULT_TIMEOUT;
import static com.smedi.ismedi.authority.starter.common.Constant.REDIS_DEFAULT_TIMEUNIT;

/**
 * redis工具类 （与历史版本RedisUtil不兼容）
 *
 * @author jiangqinxun
 * @date 2022/8/14 16:21
 */
@Component
@Deprecated
@AllArgsConstructor
public class RedisStringHelper {
    private final StringRedisTemplate stringRedisTemplate;
    private final JsonHelper jsonHelper;

    public void set(String key, Object object, long timeout, TimeUnit unit) {
        String json = jsonHelper.objectToJson(object);
        stringRedisTemplate.opsForValue().set(key, json, timeout, unit);
    }

    public void set(String key, Object object, long timeout) {
        String json = jsonHelper.objectToJson(object);
        this.set(key, json, timeout, REDIS_DEFAULT_TIMEUNIT);
    }

    public void set(String key, Object object) {
        String json = jsonHelper.objectToJson(object);
        this.set(key, json, REDIS_DEFAULT_TIMEOUT, REDIS_DEFAULT_TIMEUNIT);
    }

    public <T> T get(String key, Class<T> tClass) {
        Object result = this.get(key);
        if (null == result) {
            return null;
        }
        String json = result.toString();
        return jsonHelper.jsonToObject(json, tClass);
    }

    public Object get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
}
